package com.cskaoyan.service.wx_comment;

import com.cskaoyan.bean.wx_comment.Comment;
import com.cskaoyan.bean.wx_comment.CommentExample;
import com.cskaoyan.bean.wx_comment.User;
import com.cskaoyan.bean.wx_comment.UserExample;
import com.cskaoyan.bean.wx_comment.vo.CommentCountVo;
import com.cskaoyan.bean.wx_comment.vo.CommentListVo;
import com.cskaoyan.exception.wx_comment.WxCommentEmptyException;
import com.cskaoyan.exception.wx_comment.WxUserEmptyException;
import com.cskaoyan.mapper.wx_comment.UserMapperByWxComment;
import com.cskaoyan.mapper.wx_comment.WxCommentMapper;
import com.cskaoyan.utils.ReflectionSetJavaBeanUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 类<code>Doc</code>用于：TODO
 *
 * @author Acher
 * @version 1.0
 * @date 2021-10-23
 */
@Service
public class WxCommentServiceImpl implements WxCommentService {

    @Autowired
    WxCommentMapper wxCommentMapper;
    @Autowired
    UserMapperByWxComment userMapper;

    @SneakyThrows
    @Override
    public CommentCountVo count(Comment comment) {
        // valueId: 1181021
        // type: 0
        // 根据valueId和type查comment表，与关系
        CommentExample example = new CommentExample();
        example.createCriteria()
                .andValueIdEqualTo(comment.getValueId())
                .andTypeEqualTo(comment.getType());
        List<Comment> comments = wxCommentMapper.selectByExample(example);
        if (comments == null || comments.size() == 0)
            throw new WxCommentEmptyException("评论空空如也~🦑");
        int hasPicCount = (int) comments.stream()
                .filter(Comment::getHasPicture)
                .count();
        return new CommentCountVo(hasPicCount, comments.size());
    }

    @SneakyThrows
    @Override
    public CommentListVo list(Comment comment) {
        // valueId=1181000&type=0
        CommentExample commentexample = new CommentExample();
        commentexample.createCriteria()
                .andValueIdEqualTo(comment.getValueId())
                .andTypeEqualTo(comment.getType());
        List<Comment> comments = wxCommentMapper.selectByExample(commentexample);
        if (comments == null || comments.size() == 0)
            throw new WxCommentEmptyException("暂时还没有评论，快去评论吧");
        List<Integer> userIds = new ArrayList<>();
        comments.forEach(item -> userIds.add(item.getUserId()));
        UserExample userExample = new UserExample();
        userExample.createCriteria().andIdIn(userIds);
        List<User> users = userMapper.selectByExample(userExample);
        if (users == null || users.size() == 0)
            throw new WxUserEmptyException("用户不存在");
        // 建立两个list的映射关系 comments 👉 users
        List<CommentListVo.ListDTO> list = new ArrayList<>();
        for (Comment item : comments) {
            CommentListVo.ListDTO listDTO = new CommentListVo.ListDTO();
            for (User user : users) {
                if (item.getUserId() == user.getId()) {
                    listDTO.setUserInfo(new CommentListVo
                            .ListDTO
                            .UserInfoDTO(user.getNickname(), user.getAvatar()));
                    break;
                }
            }
            listDTO.setAddTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    .format(item.getAddTime()));
            listDTO.setContent(item.getContent());
            listDTO.setAdminContent(item.getAdminContent());
            if (item.getHasPicture()) {// ["",""]
                listDTO.setPicList(Arrays.asList(item.getPicUrls()));
            }
            list.add(listDTO);
        }
        return new CommentListVo(2, 1, 20, 1, list);
    }

    @Override
    public boolean add(Comment comment) {
        ReflectionSetJavaBeanUtils.setJavaBean(comment);
        return wxCommentMapper.insert(comment) == 1;
    }
}
